<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />

    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="picCode" class="inp" maxlength="4" placeholder="请输入图形验证码" type="text" >
          <img v-if="picUrl" :src="picUrl" alt="" @click="getPicCode">
        </div>
        <div class="form-item">
          <input v-model="msgCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode">
            {{totalSecond===second?'获取验证码':second+'秒后重新获取'}}
          </button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
// 按需要导入api里面的请求方法
import { getPicCode, getMsgCode, codeLogin } from '@/api/login'
// import { Toast } from 'vant'

export default {
  name: 'LoginPage', // 组件名字一般要求不能只是单个字母
  created () {
    this.getPicCode()
  },
  data () {
    return {
      picKey: '', // 保留这个是因为手机验证码那边需要，，存储图片唯一标识
      picUrl: '', /// /获取图形验证码后存储图片路径
      totalSecond: 60,
      second: 60,
      timer: null,
      picCode: '', // 用户输入的要拿去后台验证的图像验证码
      mobile: '',
      msgCode: ''

    }
  },
  methods: {
    // 异步获取图形验证码方法
    async getPicCode () {
      // 利用解构获取后台来的数据
      const { data: { base64, key } } = await getPicCode()
      // console.log(base64, key)
      this.picUrl = base64
      this.picKey = key
      // this.$toast.success('获取图形验证码成功！')
    },
    validFn () {
      // 判断手机号和图像验证码的格式、正确返回true,错误返回false
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast.fail('请输入正确手机号码！')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast.fail('请输入正确图形验证码！')
        return false
      }
      return true
    },
    async getCode () {
      if (!this.validFn()) {
        return
      }
      // 当前没有定时器就开启
      if (!this.timer && this.totalSecond === this.second) {
        // 发起短信验证码
        await getMsgCode(this.picCode, this.picKey, this.mobile)

        this.$toast.success('成功获取验证码')

        // 倒计时
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            // 清楚定时器变量
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
          }
        }, 1000)
      }
    },
    async login () {
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}$/.test(this.msgCode)) {
        this.$toast.fail('请输入正确验证码')
        return
      }
      console.log('开始登录')
      const result = await codeLogin(this.mobile, this.msgCode)

      console.log(result)
      // 将请求过来的数据加载到vuex中
      this.$store.commit('user/setUserInfo', result.data)
      this.$router.push('/')
      this.$toast('登录成功')
    }
  },
  destroyed () {
    clearInterval(this.timer)
  }

}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
